/********** 各种验证 ********/
//各种flag
var user_flag = false,pwd_flag = false,re_user_flag = false,re_pwd_flag = false,email_flag = false,yzm_flag = false;
//正则用户名
function username(name){
    if(!/^[a-z0-9]{4,25}$/gi.test(name)){
        Dialog.init('用户名不符合规则！',{maskClick : 1});
        user_flag = false;
        re_user_flag = false;
    }else{
        if(/luke|caoshen/gi.test(name)){
            Dialog.init('用户名不要瞎起！',{maskClick : 1});
            user_flag = false;
            re_user_flag = false;
        }
        user_flag = true;
        re_user_flag = true;
        console.log(re_user_flag);
    }
}

//正则密码
function pass(pwd){
    if(!/^[a-z0-9\_\,\.\*]{6,}$/gi.test(pwd)){
        Dialog.init('密码不符合规则！',{maskClick : 1});
        pwd_flag = false;
        re_pwd_flag = false;
    }else{
        pwd_flag = true;
        re_pwd_flag = true;
        console.log(re_pwd_flag);
    }
}

//用户名验证
$('#user').blur(function () {
    username($(this).val());
});
$('#reuser').blur(function () {
    username($(this).val());
});

//密码验证
$('#pass').blur(function () {
    pass($(this).val());
});
$('#repass').blur(function () {
    pass($(this).val());
});

//验证邮箱是否被使用
function yzyx() {
        //ajax 提交
        $.ajax({
            type: 'post',
            url: '/back/login.php?type=yzyx',
            data: {'email':$('#email').val()},
            success: function (msg) {
                if(msg != 1){
                    Dialog.init(msg, 2000);
                    email_flag = false;
                }else{
                    email_flag = true;
                }
            }
        });
}

//邮箱验证
$('#email').blur(function () {
    if(!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test($(this).val())){
        Dialog.init('请输入正确的邮箱！',{maskClick : 1});
        email_flag = false;
    }else{
        yzyx();
        console.log(email_flag);
    }
});

//验证码验证
$('#yzm').blur(function () {
    if(!/\d{4}/.test($(this).val())){
        Dialog.init('请输入4位数字的验证码！',{maskClick : 1});
        yzm_flag = false;
    }else{
        yzm_flag = true;
        console.log(yzm_flag);
    }
});

//---------------提交验证
//登录提交
$('#login').click(function () {
    if(!user_flag || !pwd_flag){//信息不完整
        Dialog.init('请输入正确的用户名或密码！',{maskClick : 1});
    }else{
        //ajax 提交
        $.ajax({
            type:'post',
            url:'/back/login.php?type=login',
            data:{'username':$('#user').val(),'pwd':$('#pass').val()},
            success: function (msg) {
                if(msg == 1){
                    Dialog.init('登录成功！',2000);
                    setTimeout('location.href="/index1.php"',2000);
                }else{
                    Dialog.init(msg,2000);
                }
            }
        });
    }
});

//注册提交
$('#register').click(function () {
    if(re_user_flag && re_pwd_flag && email_flag && yzm_flag){//信息正确
        //ajax 提交
        $.ajax({
            type:'post',
            url:'/back/login.php?type=reg',
            data:{'username':$('#reuser').val(),'pwd':$('#repass').val(),'email':$('#email').val(),'yzm':$('#yzm').val()},
            success: function (msg) {
                if(msg == 1){
                    Dialog.init('注册成功！',2000);
                    setTimeout('location.reload()',2000);
                }else{
                    Dialog.init(msg,2000);
                }
            }
        });
    }else{
        Dialog.init('请输入正确注册信息！',{maskClick : 1});
    }
});

//获取验证码函数
function getYZM() {
    //检测邮箱
    if (!email_flag) {
        Dialog.init('请输入邮箱！', {maskClick: 1});
    } else {
        //解除事件
        $('#getyzm').off();
        //ajax 提交
        $.ajax({
            type: 'post',
            url: '/back/login.php?type=yzm',
            data: {'email':$('#email').val()},
            success: function (msg) {
                Dialog.init(msg, 2000);
            }
        });
    }
}
$('#getyzm').click(function () {
    getYZM();
    //倒计时
    var time = 60;
    var t = setInterval(function () {
        $('#getyzm').val(time + '秒后重新发送');
        time--;
        if(time <= 0){
            $('#getyzm').click(getYZM);
            $('#getyzm').val('再次发送');
            clearInterval(t);
        }
    },1000);
});

//忘记密码
$('#wangji').click(function () {
    //检测用户名是否填写
    if(user_flag){
        $(this).off();
        //ajax发送用户名到后台
        $.ajax({
            type:'post',
            url:'/back/login.php?type=aaa',
            data:{'username':$('#user').val()},
            success: function (msg) {
                if(msg == 1){
                    Dialog.init('邮件已发送，请前往邮箱更改密码！',{maskClick : 1});
                }else{
                    Dialog.init('邮件发送失败，请稍后再试！',{maskClick : 1});
                }
            }
        });
    }else{
        Dialog.init('请输入正确的用户名！',{maskClick : 1});
    }
});


